import QtQuick 1.0
import "../Delegates/"
import "../WebAccess/PictureAPI.js" as PictureAPI
import "../Util/Util.js" as Util

//つぶやき系データ用
ListViewBase {
    id: _root
    x: 0
    y: 0
    height: parent.height
    width: parent.width

    scrollBarBaseColor: _Setting.scrollBarBaseColor
    scrollBarColor: _Setting.scrollBarColor
    scrollBarOpacity: _Setting.scrollBarOpacity

    density: _Device.density

    property string oauthScreenName: ""

    // 左クリックした時の処理
    // 必須の共通メソッド
    // Item object = currentItem;   これデリゲート
    function clickLeft(object){
        // クリックアイテムをハイライトする
        currentIndex = indexAt(object.x, object.y);
    }

    // 右クリックした時の処理
    function clickRight(object){
        //情報を一時保存
        _selectItemInfo.itemId = object.itemId;
        _selectItemInfo.screenName = object.screenName;
        _selectItemInfo.message = object.itemText;//Util.decodeHtmlSymbol(object.itemText);
        _selectItemInfo.isFavorite = (object.isFavorite === "true") ? true : false;
        // 位置を作る
        if(_selectItemInfo.mouse_x < 0){
            _selectItemInfo.mouse_x = object.width/2;
        }
        if(_selectItemInfo.mouse_y < 0){
            _selectItemInfo.mouse_y = object.y + _root.parent.y + object.height/3 - contentY;
        }

        //メニューを出す
        _menuContainer.state = "MenuContextTimeline";
    }

    delegate: Delegate{
        id: _delegate
        width: _root.width
        acceptedButtons: Qt.LeftButton | Qt.RightButton

        itemIndex: _status_id
        itemId: makeItemId()
        itemTitle: makeItemTitle()
        itemMessage: makeItemMessage()
        itemInformation: makeItemInfomation()
        iconPath: isRetweet ? _rts_icon : _icon
        itemForMe: makeItemForMe()

        //保存用
        property bool isRetweet: (_rts_screen_name.length !== 0)
        property string screenName: isRetweet ? _rts_screen_name : _screen_name        //Screen name
        property string inReplyToStatusId: _in_reply_to_status_id
        property string inReplyToScreenName: _in_reply_to_screen_name
        property string itemText: isRetweet ? _rts_text : _text
        property string isFavorite: (_favorited === undefined) ? "false" : _favorited
        property variant urlList: null      //URLのリスト

        //IDを成形する
        function makeItemId(){
            return _status_id;
        }

        //タイトルを成形する
        function makeItemTitle(){
            var ret = _screen_name + " / " + _name;
            if(isRetweet){
                ret = _rts_screen_name + " / " + _rts_name;
            }
            return ret;
        }

        //本文を成形する
        function makeItemMessage(){
            var text = isRetweet ? _rts_text : _text;

            //URLを正規表現で探す
            var urls = null;
            if(_urls.length === 0){
                urls = Util.pickUpUrls(_text);
            }else{
                urls = Util.pickUpUrls(_urls);
            }
            if(urls === null){
            }else{
                //画像があれば指定する
                PictureAPI.getPcitureUrl(urls, this);
                //URLを保存
                urlList = urls;

                //URLをリンクにする
                for(var i=0; i<urls.length; i++){
                    text = text.replace(urls[i], "<a href='" + urls[i] + "'>" + urls[i] + "</a>");
                }

            }
            //記号を戻す
//            text = Util.decodeHtmlSymbol(text);

            return text;
        }

        //情報を成形する
        function makeItemInfomation(){
            var ret = _date;
            var dd = new Date(_date);

            ret = Qt.formatDateTime(dd, "yyyy/MM/dd hh:mm:ss");

            if(_source.length !== 0){
                ret += " / " + ( isRetweet ? _rts_source : _source );
            }
            if(inReplyToStatusId.length !== 0){
                ret += "<br />\nIn Reply To : " + inReplyToScreenName;
            }
            if(isRetweet){
                ret += "<br />\nRetweeted by : " + _screen_name;
            }

            return ret;
        }

        // 自分の＠が含まれるか
        function makeItemForMe(){
            var ret = false;
            var screen_names = Util.pickUpScreenNames(isRetweet ? _rts_text : _text);
            var my_name = oauthScreenName.toLowerCase();

            if(screen_names === null){
            }else{
                for(var i=0; i<screen_names.length; i++){
                    if(my_name.indexOf(screen_names[i].toLowerCase()) >= 0){
                        ret = true;
                        break;
                    }
                }
            }

            return ret;
        }

        onClicked: {
//            Util.outputDebugString("onClicked:"
//                                   + ",index=" + currentIndex
//                                   + ",indexAt=" + indexAt(_delegate.x, _delegate.y)
//                                   + ",itemId=" + itemId
//                                   + ",screenName=" + screenName
//                                   );

            // 上位にクリックイベント通す
            _root.itemClicked(mouse);

            // クリックしたらフォーカス移動
            _root.forceActiveFocus();

            switch(itemIndex){
            case 0:
                break;
            case 1:
                break;
            case 2:
                break;
            default:
                break;
            }

            //マウス情報保存
            // _root.parent.parent.parent = _loaderContent
            _selectItemInfo.mouse_x = _root.parent.parent.parent.x + mouse.x;
            _selectItemInfo.mouse_y = mouse.y + _delegate.y + _root.parent.y - contentY;
            _selectItemInfo.container = _root.parent;

            //次の動作
            switch(prevPressedButtons){
            case Qt.LeftButton:
                clickLeft(_delegate);
                break;
            case Qt.RightButton:
                //メニューを出す
                clickRight(_delegate);
                break;
            default:
                break;
            }
        }

    }

}
